﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
 
    public partial class JLTAB05DOC
    {
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        #region ==KEY===========
        public static readonly DependencyProperty KEYProperty = DependencyProperty.Register("KEY",
        typeof(string), typeof(JLTAB05DOC),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKEYChanged));
        public string KEY
        {
            get
            {
                return (string)GetValue(KEYProperty);
            }
            set
            {
                SetValue(KEYProperty, value);
            }
        }
        private static void OnKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTAB05DOC;
            if (src != null)
            {
                src.KEY = (string)e.NewValue;
            }
        }
        #endregion

       
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTAB05DOCData m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataSet setDB = new DataSet();
        public DataSet setREPORT = new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public DataRow dwABFLOGE;
        public DataRow dwABP;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        VABPOS _VAB;
        public VABPOS VAB
        {
            get
            {
                if (_VAB == null) _VAB = new VABPOS();
                return _VAB;
            }
        }
        
        public JLTAB05DOC()
        {
            InitializeComponent();
            this.Name = "后整理单证";
            if (app.User.Trim() == "JLTV")
            {
                txtKRS.Text = "HZ";
                txtLFD.Text = "7269";
            }
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new JLTAB05DOCData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("KRS", typeof(string));
            tableUI.Columns.Add("LFD", typeof(string));
            tableUI.Columns.Add("POS", typeof(string));
            tableUI.Columns.Add("LOT", typeof(string));
            tableUI.Columns.Add("FOLGE", typeof(string));
            tableUI.Columns.Add("ART", typeof(string));
            tableUI.Columns.Add("VAR", typeof(string));
            tableUI.Columns.Add("FARBE", typeof(string));
            tableUI.Columns.Add("BESCHICH", typeof(string));
            
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow();
                InitDataBinding();
                txtKRS.Focus();
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    setDB.Clear();
                    setDB.AcceptChanges();
                    setREPORT.Clear();
                    setREPORT.AcceptChanges();
                    txtART.Text = "";
                    txtVAR.Text = "";
                    txtFARBE.Text = "";
                    txtBESCHICH.Text = "";
                    string KRS = txtKRS.Text.Trim();
                    string LFD = m_Data.Right("000000"+txtLFD.Text.Trim(),6);
                    string POS = m_Data.Right("000" + tbPOS.Text.Trim(), 3);
                    if (KRS == "")
                    {
                        MessageBox.Show("请输入订单号！");
                        return;
                    }
                    if (LFD == "000000")
                    {
                        MessageBox.Show("请输入订单号！");
                        return;
                    }
                    DataRow dwData = VAB.TBMain.NewRow();
                    dwData["ABPMANDANT"] = app.Mandant;
                    dwData["ABPAUFKRS"] = KRS;
                    dwData["ABPAUFLFD"] = LFD;
                    dwData["ABPAUFPOS"] = POS; 
                    setResult = VAB.QABP42(dwData); 
                    setUI.Merge(setResult);
                    ABPOSDg.DataContext = setUI.Tables["ABPOS"];
                    ABPOSDg.Items.MoveCurrentToFirst();
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();                
                setREPORT.Clear();
                setREPORT.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                dwUI["KRS"] = KRS;
                dwUI["LFD"] = LFD;
                setResult = m_Data.REPORT(dwUI);
                setREPORT.Merge(setResult);

                m_DataX = new JLTAB05DOCData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                JLTAB05DOCReport m_rpt = new JLTAB05DOCReport(dicParamater);
                m_rpt.dsSource = setREPORT;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.LAYOUTTYPE = "2";
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            
        }
                
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void btnMACHINE_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();
                setREPORT.Clear();
                setREPORT.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                string FOLGE = tbFOLGE.Text.ToString().Trim();
                dwUI["KRS"] = KRS;
                dwUI["LFD"] = LFD;
                dwUI["FOLGE"] = FOLGE;
                setResult = m_Data.MACHINE(dwUI);
                setREPORT.Merge(setResult);

                m_DataX = new JLTAB05DOCData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                JLTAB05DOCReport m_rpt = new JLTAB05DOCReport(dicParamater);
                m_rpt.dsSource = setREPORT;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.LAYOUTTYPE = "3";
                m_rpt.folge = FOLGE;
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnSAVE_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_Data.SAVE(setUI);
                MessageBox.Show("保存成功！");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
        private void ABPOSDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                {
                    KEY = "";
                    return;
                }
                    
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.tbPOS.Text = dwABPOS["ABPAUFPOS"].ToString();
                this.txtART.Text = dwABPOS["ABPART"].ToString();
                this.txtVAR.Text = dwABPOS["ABPVAR"].ToString();
                this.txtFARBE.Text = dwABPOS["ABPFARBE"].ToString();
                this.txtBESCHICH.Text = dwABPOS["ABPBESCHICH"].ToString();
                KEY = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                ReloadABFOLGE(dwABPOS);
                //ReloadABP(dwABPOS);
                ReloadABARTE(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ReloadABFOLGE(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            if (setUI.Tables.Contains("ABFOLGE"))
            {
                setUI.Tables.Remove("ABFOLGE");
                setUI.AcceptChanges();
            }
            setResult = m_Data.GetABFOLGE(dwABPOS); 
            setUI.Merge(setResult);
            ABFOLGEDg.DataContext = setUI.Tables["ABFOLGE"];


        }
        
        private void ABFOLGEDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABFLOGE = drv.Row;
                this.tbFOLGE.Text = dwABFLOGE["ABFAFOLGE"].ToString();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ReloadABE(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            if (setDB.Tables.Contains("ABE"))
            {
                setDB.Tables.Remove("ABE");
                setDB.AcceptChanges();
            }
            setResult = m_Data.GetABE(dwABPOS);
            setDB.Merge(setResult);
            ABEDg.DataContext = setDB.Tables["ABE"];


        }
        private void ABPDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABP = drv.Row;
                ReloadABE(dwABP);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ReloadABARTE(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            if (setUI.Tables.Contains("ABARTE"))
            {
                setUI.Tables.Remove("ABARTE");
                setUI.AcceptChanges();
            }
            setResult = m_Data.GetABARTE(dwABPOS);
            setUI.Merge(setResult);
            ABARTEDg.DataContext = setUI.Tables["ABARTE"];


        }

        private void btnQueryTech_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setDB.Clear();
                    setDB.AcceptChanges();
                    string ART = txtART.Text.Trim();
                    string VAR = m_Data.Left(txtVAR.Text.Trim() + "   ", 3);
                    string FARBE = txtFARBE.Text.Trim();
                    string BESCHICH = txtBESCHICH.Text.Trim(); 
                    string KRS = txtKRS.Text.Trim();
                    string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                    string POS = m_Data.Right("000" + tbPOS.Text.Trim(), 3);
                    if (KRS == "")
                    {
                        KRS = "AA";
                    }
                    if (ART == "")
                    {
                        MessageBox.Show("请输入品种号！");
                        return;
                    }
                    if (FARBE == "")
                    {
                        MessageBox.Show("请输入颜色！");
                        return;
                    }
                    if (BESCHICH == "")
                    {
                        MessageBox.Show("请输入后整理方式！");
                        return;
                    }
                    dwUI["KRS"] = KRS;
                    dwUI["LFD"] = LFD;
                    dwUI["POS"] = POS;
                    dwUI["ART"] = ART;
                    dwUI["VAR"] = VAR;
                    dwUI["FARBE"] = FARBE;
                    dwUI["BESCHICH"] = BESCHICH;
                    setResult = VAB.QABP43(dwUI);
                    setDB.Merge(setResult);
                    ABPDg.DataContext = setDB.Tables["ABPPOS"];
                    ABPDg.Items.MoveCurrentToFirst();
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnCOPY_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setREPORT.Merge(setUI);
                setREPORT.Merge(setDB);
                m_Data.COPY(setREPORT);
                MessageBox.Show("工艺参数已成功拷贝！");
                btnQuery_Click(null,null);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnFPTECH_Click(object sender, RoutedEventArgs e)
        {
            try
            {                
                DataSet setResult = new DataSet();
                setREPORT.Clear();
                setREPORT.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                dwUI["KRS"] = KRS;
                dwUI["LFD"] = LFD;
                setResult = m_Data.REPORT(dwUI);
                setREPORT.Merge(setResult);

                m_DataX = new JLTAB05DOCData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                JLTAB05DOCReport m_rpt = new JLTAB05DOCReport(dicParamater);
                m_rpt.dsSource = setREPORT;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.LAYOUTTYPE = "1";
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnMM_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();
                setREPORT.Clear();
                setREPORT.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                string FOLGE = tbFOLGE.Text.ToString().Trim();
                dwUI["KRS"] = KRS;
                dwUI["LFD"] = LFD;
                dwUI["FOLGE"] = FOLGE;
                setResult = m_Data.MACHINE(dwUI);
                setREPORT.Merge(setResult);

                m_DataX = new JLTAB05DOCData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                JLTAB05DOCReport m_rpt = new JLTAB05DOCReport(dicParamater);
                m_rpt.dsSource = setREPORT;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.LAYOUTTYPE = "4";
                m_rpt.folge = FOLGE;
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void AB05CardCommand(object sender, RoutedEventArgs e) 
        {
              
        
            DataSet Set = new DataSet();
          //  DataRow dwWB = Set.Tables["WBBST"].Rows[0];
            Set.Merge(setUI);
            Set.Merge(cABF.TBMain);
            Set.Merge(cWB.TBMain); 
            cDR.PRINT(Set);
        }

        private void btnZM_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();
                setREPORT.Clear();
                setREPORT.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = m_Data.Right("000000" + txtLFD.Text.Trim(), 6);
                string FOLGE = tbFOLGE.Text.ToString().Trim();
                dwUI["KRS"] = KRS;
                dwUI["LFD"] = LFD;
                dwUI["FOLGE"] = FOLGE;
                setResult = m_Data.MACHINE(dwUI);
                setREPORT.Merge(setResult);

                m_DataX = new JLTAB05DOCData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                JLTAB05DOCReport m_rpt = new JLTAB05DOCReport(dicParamater);
                m_rpt.dsSource = setREPORT;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.LAYOUTTYPE = "5";
                m_rpt.folge = FOLGE;
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
